package com.wenc.psi.atvt;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.wenc.psi.R;
import com.wenc.psi.databinding.ActivityHomeBinding;
import com.wenc.psi.fgmt.HomeFragment;
import com.wenc.psi.fgmt.InventoryFragment;
import com.wenc.psi.fgmt.PersonFragment;
import com.wenc.psi.fgmt.StatisticsFragment;
import com.wenc.psi.vmd.PersonViewModel;

public class HomeActivity extends AppCompatActivity {

    private PersonViewModel personViewModel;
    private ActivityHomeBinding binding;
    private Fragment[] fragments;
    private int currFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 获取DataBinding
        binding = DataBindingUtil.setContentView(this, R.layout.activity_home);
        binding.setLifecycleOwner(this);
        // 构造fragments数组
        fragments = new Fragment[]{
                new HomeFragment(), // 首页
                new InventoryFragment(), // 库存管理
                new StatisticsFragment(), // 统计查询
                new PersonFragment()}; // 个人信息
        // 添加首页fragment
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fragmeng_container, fragments[0])
                .commit();
        // 记录当前fragment
        currFragment = 0;
        // 初始化控件
        initView();
        // 获取personViewModel
        personViewModel = new ViewModelProvider(this).get(PersonViewModel.class);
        personViewModel.getEmployeeById(getIntent().getStringExtra("employeeId"));
    }

    // 初始化控件
    private void initView(){
        // 对BottomNavigationView的选择监听
        binding.bottomNavigationView.setOnNavigationItemSelectedListener((MenuItem item) -> {
            // 设为选中模式
            item.setChecked(true);
            int id = item.getItemId();
            if (id == R.id.bt_homePage) switchFragment(0);
            else if (id == R.id.bt_inventoryPage) switchFragment(1);
            else if (id == R.id.bt_statisticsPage) switchFragment(2);
            else if (id == R.id.bt_personPage) switchFragment(3);
            return false;
        });
    }

    // 切换Fragment
    private void switchFragment(int nextFragment) {
        // 如果点击的为当前的fragment则返回
        if (nextFragment == currFragment) return;
        // 获取事务
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        // 如果点击的还未被添加则添加
        if (!fragments[nextFragment].isAdded())
            transaction.add(R.id.fragmeng_container, fragments[nextFragment]);
        // 隐藏当前的fragment
        transaction.hide(fragments[currFragment]);
        // 显示下一个fragment
        transaction.show(fragments[nextFragment]);
        // 记录当前fragment
        currFragment = nextFragment;
        // 提交事务
        transaction.commit();
    }
}
